package com.order.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CUDBUtil {
	static String driver="org.gjt.mm.mysql.Driver";
	 static String url="jdbc:mysql://localhost:3306/test";
	 static	String user="root";	
		public static void addcg(int i, String vtname,String vtbz)
		{
			try
			{
				Class.forName(driver);
				Connection con=DriverManager.getConnection(url,user,"");
				Statement st=con.createStatement();
				String s=String.valueOf(i);
				String sql="insert into CountUnit values('"+s+"','"+vtname+"','"+vtbz+"');";
				st.executeUpdate(sql);
				st.close();
				con.close();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		public static void delcg(String vtname)
		{
			try
			{
				Class.forName(driver);
				Connection con=DriverManager.getConnection(url,user,"");
				Statement st=con.createStatement();
				String sql="delete from CountUnit where unit_name='"+vtname+"'";
				st.executeUpdate(sql);
				st.close();
				con.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		public static List<String[]> getlb()
		{
			List<String[]>ll=new ArrayList<String[]>();
			try
			{
				Class.forName(driver);
				Connection con=DriverManager.getConnection(url,user,"");
				Statement st=con.createStatement();
				String sql="select * from CountUnit";
				ResultSet rs=st.executeQuery(sql);
				while(rs.next())
				{
					String[] s=new String[2];
					for(int i=0;i<s.length;i++)
					{
						s[i]=rs.getString(i+2);
					}
					ll.add(s);
				}
				rs.close();
				st.close();
				con.close();
				
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			return ll;
		}
	public static  void main(String args[])
	{
		List<String[]> ll=CUDBUtil.getlb();
		for(String[] s:ll)
		{
			for(int i=0;i<s.length;i++)
			{
				System.out.print(s[i]);
			}
		}
	}
	public static String getmaxid()
	{
		String str=null;
		try
		{
			Class.forName(driver);
			Connection con=DriverManager.getConnection(url,user,"");
			Statement st=con.createStatement();
			String sql="select max(unit_id) from CountUnit";
			ResultSet rs=st.executeQuery(sql);
		    rs.next();
		    str=rs.getString(1);
		    System.out.println(str);
		    rs.close();
		    st.close();
		    con.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return str;
	}
}